<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Completion extends CI_Controller {

	/**
	 * Array that contains the validation rules
	 * @access protected
	 * @var array
	 */
	protected $validation_rules = array(
		array(
			'field' => 'requests',
			'label' => 'Requests',
			'rules' => 'trim|required|max_length[4]|greater_than[1]|less_than[3001]'
		),
		array(
			'field' => 'methods',
			'label' => 'methods',
			'rules' => 'trim|required'
		),
		array(
			'field' => 'min_value',
			'label' => 'min value',
			'rules' => 'trim|required|max_length[8]|min_length[1]'
		),
		array(
			'field' => 'response_time',
			'label' => 'completion time',
			'rules' => 'trim|required'
		));
	
	/**
	 * Controller constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->model('completion_m');
		
	}
	
	/**
	 * Default controller method, shows the default respose time page
	 * @access public
	 * @return 
	 */
	public function index()
	{
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules($this->validation_rules);
		
		$this->data->methods = $this->completion_m->get_methods();
		
		if($this->data->methods)
		{
			$this->data->minMaxTime = prepareMinMaxTimes($this->completion_m->get_min_max_time());
		}
		//Set custom errors for the features input
		$this->form_validation->set_message('greather_than', 'Please enter a value within the allowed limits.');
		$this->form_validation->set_message('less_than', 'Please enter a value within the allowed limits');
		
		if($this->form_validation->run())
		{
			$this->data->xml = array(
					'requests' => $this->input->post('requests'),
					'over' => $this->input->post('min_value'),
					'from' => strtotime($this->input->post('response_time')),
					'method' => base64_encode($this->input->post('methods')));			
		} else {
			
			$this->data->xml = array(
					'requests' => '1000',
					'over' => '1',
					'from' => $this->completion_m->get_last_time(),
					'method' => base64_encode('all'));
		}
		
		
		$this->data->main_content = "pages/completion";
		$this->load->view('template/template.php', $this->data);
	
	
	}
	
	/**
	 * Returns the instrumented data for the given
	 * parameters, in XML format, compatible with the chart.
	 * @access public
	 * @return 
	 */
	public function xml()
	{
		//Get graph parameters from the URI
		$requests = $this->uri->segment(3);
		$over = $this->uri->segment(4);
		$from = $this->uri->segment(5);
		$method = base64_decode($this->uri->segment(6));
		
		$query = $this->completion_m->get_data($requests, $over, $from, $method);
		
		if($query->num_rows > 0)
		{
			// Initiate the xml library
		    $xml = new Xml_writer;
		    $xml->setRootName('chart');
		    $xml->initiate();
		    
		    $xml->startBranch('series');
		    $idIncrementer = 0;
		    
		    foreach($query->result() as $row)
		    {
		    	$key = explode(".", $row->requestTime);
				$xml->addNode('value', date("M j Y G:i:s", $key[0]), array('xid' => $idIncrementer));
				$idIncrementer++;
		    }
		    
			$xml->endBranch();
			$xml->startBranch('graphs');
		    $xml->startBranch('graph', array('gid' => '1'));
		    
		    $idIncrementer = 0;
		    foreach($query->result() as $row)
			{
		    	$xml->addNode('value', $row->completionTime, array('xid' => $idIncrementer));
		    	$idIncrementer++;
			}
			
			$xml->endBranch();
		    $xml->endBranch();
		    
		    $this->output->set_content_type('content-type: text/xml');
		    //Return the xml
		    $xml->getXml(true);
		} else {
			   
			$this->output->set_content_type('content-type: text/xml');
			echo '<?xml version="1.0" encoding="UTF-8"?>
				<chart> 
				    <series> 
				        <value xid="0">O results</value> 
				    </series> 
				    <graphs> 
				        <graph gid="1"> 
				            
				        </graph> 
				    </graphs> 
				</chart>';
		}	
	}
}

/* End of file completion.php */
/* Location: ./application/controllers/completion.php */